/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converters;

import entities.Province;
import facades.ProvinceFacade;
import javax.faces.bean.ApplicationScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author damfercer
 */

@Named
@ApplicationScoped
public class ProvinceConverter implements Converter{
    /*@Inject
    @ResourceList
    Map<Integer,Province> provincesMap;*/
    @Inject
    private ProvinceFacade provinceFacade;
    
    @Override
    public Province getAsObject(FacesContext context, UIComponent component, String value) {
        Province province=null;
        try {
            Integer i=new Integer(value);
            //province=provincesMap.get(i);
            province=provinceFacade.find(i);
        } catch (NumberFormatException e) {
        }
        return province;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String res=null;
        Province province=null;
        if(value.getClass().equals(Province.class)){
            province=(Province)value;
        }
        if(province!=null){
            res=province.getIdProvince().toString();
        }
        return res;
    }
    
}
